<?php

// +--------------------------------------------------------------------------+
// | Media Gallery WKZ Plugin - Geeklog                                       |
// +--------------------------------------------------------------------------+
// | autoinstall.php                                                          |
// |                                                                          |
// | This file provides helper functions for the automatic plugin install.    |
// +--------------------------------------------------------------------------+
// | Copyright (C) 2002-2008 by the following authors:                        |
// |                                                                          |
// | Mark R. Evans          mark AT glfusion DOT org                          |
// +--------------------------------------------------------------------------+
// |                                                                          |
// | This program is free software; you can redistribute it and/or            |
// | modify it under the terms of the GNU General Public License              |
// | as published by the Free Software Foundation; either version 2           |
// | of the License, or (at your option) any later version.                   |
// |                                                                          |
// | This program is distributed in the hope that it will be useful,          |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
// | GNU General Public License for more details.                             |
// |                                                                          |
// | You should have received a copy of the GNU General Public License        |
// | along with this program; if not, write to the Free Software Foundation,  |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.          |
// |                                                                          |
// +--------------------------------------------------------------------------+

/**
* Autoinstall API functions for the Media Gallery plugin
*
* @package Media Gallery
*/

/**
* Plugin autoinstall function
*
* @param    string  $pi_name    Plugin name
* @return   array               Plugin information
*
*/
function plugin_autoinstall_mediagallery($pi_name)
{
    $pi_name         = 'mediagallery';
    $pi_display_name = 'Media Gallery';
    $pi_admin        = $pi_name . ' Admin';
    $pi_config       = $pi_name . ' Config';

    $info = array(
        'pi_name'         => $pi_name,
        'pi_display_name' => $pi_display_name,
        'pi_version'      => '1.7.0',
        'pi_gl_version'   => '2.0.0',
        'pi_homepage'     => 'http://www.trybase.com/~dengen/log/'
    );

    $groups = array(
        $pi_admin  => 'Users in this group can administer the Media Gallery plugin',
        $pi_config => 'Users in this group can configure the Media Gallery plugin'
    );

    $features = array(
        $pi_name . '.admin'  => 'Ability to administer the Media Gallery Plugin',
        $pi_name . '.config' => 'Ability to configure the Media Gallery Plugin'
    );

    $mappings = array(
        $pi_name . '.admin'  => array($pi_admin),
        $pi_name . '.config' => array($pi_config)
    );

    $tables = array(
        'mg_albums',
        'mg_media',
        'mg_media_albums',
        'mg_usage_tracking',
        'mg_mediaqueue',
        'mg_media_album_queue',
        'mg_playback_options',
        'mg_userprefs',
        'mg_exif_tags',
        'mg_watermarks',
        'mg_category',
        'mg_sessions',
        'mg_session_items',
        'mg_session_items2',
        'mg_session_log',
        'mg_sort',
        'mg_postcard',
        'mg_rating',
    );

    $requires = array(
        array(
               'db' => 'mysql',
               'version' => '4.1'
             )
    );

    $inst_parms = array(
        'info'      => $info,
        'groups'    => $groups,
        'features'  => $features,
        'mappings'  => $mappings,
        'tables'    => $tables,
        'requires'  => $requires
    );

    return $inst_parms;
}

/**
* Load plugin configuration from database
*
* @param    string  $pi_name    Plugin name
* @return   boolean             true on success, otherwise false
* @see      plugin_initconfig_mediagallery
*
*/
function plugin_load_configuration_mediagallery($pi_name)
{
    global $_CONF;

    $base_path = $_CONF['path'] . 'plugins/' . $pi_name . '/';

    require_once $_CONF['path_system'] . 'classes/config.class.php';
    require_once $base_path . 'install_defaults.php';

    return plugin_initconfig_mediagallery();
}

/**
* Plugin postinstall
*
* We're inserting our default data here since it depends on other stuff that
* has to happen first ...
*
* @return   boolean     true = proceed with install, false = an error occured
*
*/
function plugin_postinstall_mediagallery($pi_name)
{
    global $_TABLES;

    $pi_name = 'mediagallery';

    $admin_group_id  = DB_getItem($_TABLES['groups'], 'grp_id', "grp_name = 'mediagallery Admin'");
    $config_group_id = DB_getItem($_TABLES['groups'], 'grp_id', "grp_name = 'mediagallery Config'");

    $MG_SQL = array();

    // create random image block
    $MG_SQL[] = "INSERT INTO {$_TABLES['blocks']} (is_enabled, name, type, title, blockorder, content, rdfurl, rdfupdated, onleft, phpblockfn, help, group_id, owner_id, perm_owner, perm_group, perm_members,perm_anon) VALUES (0, 'mgrandom', 'phpblock', 'Random Image',        0, '', '', 0, 1, 'phpblock_mg_randommedia','', 4, 2, 3, 3, 2, 2);";
    $MG_SQL[] = "INSERT INTO {$_TABLES['blocks']} (is_enabled, name, type, title, blockorder, content, rdfurl, rdfupdated, onleft, phpblockfn, help, group_id, owner_id, perm_owner, perm_group, perm_members,perm_anon) VALUES (0, 'mgenroll', 'phpblock', 'Member Album Enroll', 0, '', '', 0, 1, 'phpblock_mg_maenroll',   '', 4, 2, 3, 3, 2, 0);";

    // Save the grp id for later uninstall
    $MG_SQL[] = "INSERT INTO {$_TABLES['vars']} VALUES ('{$pi_name}_gid', $admin_group_id)";

    // Save the grp id for later uninstall
    $MG_SQL[] = "INSERT INTO {$_TABLES['vars']} VALUES ('{$pi_name}_cid', $config_group_id)";

    // Save the mg_last_usage_purge for usage tracking
    $MG_SQL[] = "INSERT INTO {$_TABLES['vars']} VALUES ('mg_last_usage_purge', 0)";

    foreach ($MG_SQL as $sql) {
        DB_query($sql);
        if (DB_error()) {
            COM_errorLog("SQL error in Media Gallery plugin postinstall, SQL: " . $sql);
            return false;
        }
    }

    return true;
}

/**
* Check if the plugin is compatible with this Geeklog version
*
* @param    string  $pi_name    Plugin name
* @return   boolean             true: plugin compatible; false: not compatible
*
*/
function plugin_compatible_with_this_version_mediagallery($pi_name)
{
    global $_CONF, $_DB_dbms;

    // check if we support the DBMS the site is running on
    $dbFile = $_CONF['path'] . 'plugins/' . $pi_name . '/sql/'
            . $_DB_dbms . '_install.php';
    if (! file_exists($dbFile)) {
        return false;
    }

    if (!function_exists('COM_truncate') || !function_exists('MBYTE_strpos')) {
        return false;
    }

    if (!function_exists('SEC_createToken')) {
        return false;
    }

    if (!function_exists('COM_showMessageText')) {
        return false;
    }

    return true;
}

?>
